requirejs.config({
    baseUrl: '../js/libs',
    paths: {
        jquery: 'jquery.min',
        jquery_route: 'jquery.route',
        bootstrap: "../../bootstrap-3.3.5/dist/js/bootstrap.min",
        config: "../common/config",
        common: "../common/common",
        arttemplateUtil: '../common/arttemplate-util',
        layer: '../plugins/layer/layer'
    },
    shim: { //引入没有使用requirejs模块写法的类库。backbone依赖underscore
        'jquery': {
            exports: '$'
        },
        'bootstrap': ['jquery']
    }
});
requirejs(['jquery', 'bootstrap', 'config', 'arttemplateUtil', 'common','layer'], function($, bootstrp, config, arttemplateUtil, common, layer) {
    /**
     * Created by linyamei on 2016/8/25.
     */
    var app = {
        init: function() {
            app.rendTemple();
            app.addTime();
            app.removeTime();
            app.checkMult();
            app.submit();
            $("#ret_btn").click(function(){
        		window.location.href="#logistics/deliveryset/2";
        	});
        },
        /**
         * @return {[type]} [description]
         */
        rendTemple: function() {
        	var ft_id = $("#temp_id").val()+"";//"771625281478729728";
        	if(ft_id != '' && ft_id != 0){
        		$("#btitle").html("编辑配送时间组合");
	        	var param = {ft_id:ft_id};
				common.common_ajax_request('/api/v1/getFreqTemp',param,false,function(data,code,msg){
					//console.debug(JSON.stringify(data));
					$("#timeTemplateName").val(data.temp_name);
					var freqList = data.storeFrequencyList;
					if(freqList && freqList.length > 0){
						$(".timenew").show();
						for(var i=0; i<freqList.length; i++){
							app.addRow(freqList[i]);
						}
						app.removeTime();
	                    app.checkMult();
					}
				});
        	}
        },
        /**
         * 新增时间
         */
        addTime: function() {
            $("#addsj").on("click", function() {
                //新增显示表头。
                $(".timenew").show();
                if ($("#xzsj li").length > 9) {
                	layer.alert("最多可新增配送规格次数10条！");
                    return false;
                } else {
                	app.addRow();
                    app.removeTime();
                    app.checkMult();
                }
            });
        },
        addRow: function(rowObj){
        	var i= new Date().getTime()+"_"+($("#xzsj li").length+1);
        	if(rowObj){
        		var timeArr = rowObj.times.split(",");
        		$("#xzsj").append("<li><div class='weekbox'><span></span><span class='txtValue'>"+rowObj.name+"</span><input type='hidden' idVal='"+rowObj.id+"' value='"+rowObj.times+"' class='dayVal' /></div>" +
                		"<div><input type='checkbox' data-type='checkbox' "+(timeArr.indexOf("1")>-1?"checked='checked' class='chk1'":"")+" data-value='一' value='1' id='checkbox-10-1"+i+"'/><label for='checkbox-10-1"+i+"'></label></div>" +
                		"<div><input type='checkbox' data-type='checkbox' "+(timeArr.indexOf("2")>-1?"checked='checked' class='chk1'":"")+" data-value='二' value='2' id='checkbox-10-2"+i+"'/><label for='checkbox-10-2"+i+"'></label></div>" +
                		"<div><input type='checkbox' data-type='checkbox' "+(timeArr.indexOf("3")>-1?"checked='checked' class='chk1'":"")+" data-value='三' value='3' id='checkbox-10-3"+i+"'/><label for='checkbox-10-3"+i+"'></label></div>" +
                		"<div><input type='checkbox' data-type='checkbox' "+(timeArr.indexOf("4")>-1?"checked='checked' class='chk1'":"")+" data-value='四' value='4' id='checkbox-10-4"+i+"'/><label for='checkbox-10-4"+i+"'></label></div>" +
                		"<div><input type='checkbox' data-type='checkbox' "+(timeArr.indexOf("5")>-1?"checked='checked' class='chk1'":"")+" data-value='五' value='5' id='checkbox-10-5"+i+"'/><label for='checkbox-10-5"+i+"'></label></div>" +
                		"<div><input type='checkbox' data-type='checkbox' "+(timeArr.indexOf("6")>-1?"checked='checked' class='chk1'":"")+" data-value='六' value='6' id='checkbox-10-6"+i+"'/><label for='checkbox-10-6"+i+"'></label></div>" +
                		"<div><input type='checkbox' data-type='checkbox' "+(timeArr.indexOf("7")>-1?"checked='checked' class='chk1'":"")+" data-value='日' value='7' id='checkbox-10-7"+i+"'/><label for='checkbox-10-7"+i+"'></label></div>" +
                		"<div><a href='javascript:void(0);' class='deleteli'>删除</a></div></li>");
        	}else{
        		$("#xzsj").append("<li><div class='weekbox'><span></span><span class='txtValue'></span><input type='hidden' class='dayVal' /></div>" +
                		"<div><input type='checkbox' data-type='checkbox' data-value='一' value='1' id='checkbox-10-1"+i+"'/><label for='checkbox-10-1"+i+"'></label></div>" +
                		"<div><input type='checkbox' data-type='checkbox' data-value='二' value='2' id='checkbox-10-2"+i+"'/><label for='checkbox-10-2"+i+"'></label></div>" +
                		"<div><input type='checkbox' data-type='checkbox' data-value='三' value='3' id='checkbox-10-3"+i+"'/><label for='checkbox-10-3"+i+"'></label></div>" +
                		"<div><input type='checkbox' data-type='checkbox' data-value='四' value='4' id='checkbox-10-4"+i+"'/><label for='checkbox-10-4"+i+"'></label></div>" +
                		"<div><input type='checkbox' data-type='checkbox' data-value='五' value='5' id='checkbox-10-5"+i+"'/><label for='checkbox-10-5"+i+"'></label></div>" +
                		"<div><input type='checkbox' data-type='checkbox' data-value='六' value='6' id='checkbox-10-6"+i+"'/><label for='checkbox-10-6"+i+"'></label></div>" +
                		"<div><input type='checkbox' data-type='checkbox' data-value='日' value='7' id='checkbox-10-7"+i+"'/><label for='checkbox-10-7"+i+"'></label></div>" +
                		"<div><a href='javascript:void(0);' class='deleteli'>删除</a></div></li>");
        	}
        },
        /**
         * 移除行
         * @param  {[type]} obj [description]
         * @return {[type]}     [description]
         */
        removeTime: function() {
            $('.deleteli').on("click", function() {
                $(this).parent().parent().remove();
                //删除最后一条，隐藏表头。
                if ($("#xzsj li").length < 1) {
                    $(".timenew").hide();
                }
            });
        },
        /**
         * 勾选星期
         * [checkMult description]
         * @return {[type]} [description]
         */
        checkMult: function() {
            $('[data-type="checkbox"]').click(function() {
                var disArr = [];
                var dayArr = [];
                if ($(this).prop("checked")) {
                    $(this).addClass('chk1');
                } else {
                    $(this).removeClass('chk1');
                }
                var bb=$(this).parent().parent().find('.chk1');
                $.each(bb, function(i) {
                	disArr.push("周"+$(this).attr('data-value'));
                	dayArr.push($(this).attr('value'));
                });
                $(this).parent().parent().find(".txtValue").text(disArr.join("&"));
                $(this).parent().parent().find(".dayVal").val(dayArr.join(","));
            });
        },
        /**
         * 提交
         */
        submit: function() {
            $('.btn-primary').click(function() {
            	var timeTemplateName = $("#timeTemplateName").val();
            	if(timeTemplateName == 0 || timeTemplateName == ''){
            		layer.alert("配送时间模板名称 不能为空");
            		return;
            	}
            	
            	
            	var days = $(".dayVal");
            	var freqArr = [];
            	var timeArr = [];
            	var flag= true;
            	$.each(days, function(i) {
            		//console.info($(this).attr("idVal"));
            		if($(this).val() != ''){
            			var freqJson = {};
            			if($(this).attr("idVal")){
            				freqJson.id=$(this).attr("idVal");
            			}
                    	freqJson.name = $(this).parent().find(".txtValue").text();
                    	freqJson.times = $(this).val();
                    	if(timeArr.length >0 && timeArr.indexOf($(this).val()) > -1){
                    		flag = false;
                    		return false;
                    	}
                    	timeArr.push($(this).val());
                    	
                    	freqJson.days = $(this).val().split(",").length;
                    	freqJson.frequency = 2;
                    	/////console.info(freqJson);
                    	freqArr.push(freqJson);
            		}
                });
            	
            	if(!flag){
            		layer.alert("不能存在多个同样的时间组合");
            		return;
            	}
            	if(freqArr.length == 0){
            		layer.alert("最少创建1个时间组合");
            		return;
            	}
            	
            	jQuery(this).attr('disabled','disabled');
            	
            	
            	
            	var param = {};
            	param.id=$("#temp_id").val()+"";
            	param.temp_name = timeTemplateName;
            	param.frequencyJson = JSON.stringify(freqArr);
            	
            	common.common_ajax_request('/api/v1/store_frequency_save',param,false,function(data,code,msg){
        			if(code==200 && msg.businessCode==0){
                		window.layer.alert('保存成功!', {
                			  icon: 1,closeBtn:false
                			}, function(){
                				//刷新当前页面
                				window.location.href="#logistics/deliveryset/2";
                			});
        			}else{
        				layer.alert("保存失败！原因【"+msg.businessNote+"】");
        			}
        		}, "", "post"); 
            });
        }
    };
    app.init();
});
